﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Sustainalytics.ClientDW.Entities;
using System.Web.Http.Cors;

namespace Sustainalytics.GCCS.Service.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    //[AutoInvalidateCacheOutput]
    [RoutePrefix("api/v1/gcoverallcompanysummary")]
    public class GCOverallCompanySummaryController : ApiController
    {
        IGCOverallCompanySummaryManager _manager;

        public GCOverallCompanySummaryController(IGCOverallCompanySummaryManager manager)
        {
            _manager = manager;
        }


        /// <summary>
        /// GET.
        /// </summary>
        /// <param name="companyId"></param>
        /// <param name="overallAssessment"></param>
        /// <param name="isOverallAssessmentFilterUniverseOnly"> Whether the overallAssessment is used only for universe filtering. False if is used also for filtering entities. </param>
        /// <returns></returns>
       // [CacheOutput(ServerTimeSpan = 100, ClientTimeSpan = 0, NoCache = true, MustRevalidate = true, Private = true)]
        [Route("")]
        public HttpResponseMessage Get(Guid companyId, GCAssessment? overallAssessment = null, bool isOverallAssessmentFilterUniverseOnly = true)
        {
            try
            {
                List<int> userUniverseIds = GCCSUtility.GetUniverses(overallAssessment);

                // get company summary
                GCOverallCompanySummary ent = _manager.GetByCompany(userUniverseIds, companyId, isOverallAssessmentFilterUniverseOnly ? null : overallAssessment, false);
                                
                // project to dto
                return this.Request.CreateResponse(HttpStatusCode.OK, ent != null ? new GCOverallCompanySummaryDto().LoadFrom(ent) : null);
            }
            catch (GCOverallCompanySummaryNotFoundException ex)
            {
                var dto = ex.GetHttpResponseMessageDto();
                return this.Request.CreateResponse(HttpStatusCode.NotFound, dto);
            }
        }
    }
}
